#ifndef __SV2_STREAMER__
#define __SV2_STREAMER__

#include "sms_core.h"
#include <string>
#include <chrono>
#include <mutex>
#include <queue>
#include <condition_variable>
#include "sv2_video_streamer.h"


namespace sv2
{

  class VideoStreamer;

  class StreamerCppNode : public sms::BaseNode
  {
  public:
    StreamerCppNode(
        std::string job_name,
        std::string param_file,
        std::string specified_input_topic,
        std::string ip = "127.0.0.1",
        int port = 9094) : sms::BaseNode("StreamerCppNode", job_name, param_file, ip, port),
                           _det_res_sub(specified_input_topic, "sensor_msgs::CompressedImage", std::bind(&StreamerCppNode::full_res_callback, this, std::placeholders::_1))
    {
      // 读取节点参数
      this->_image_width = this->get_param("image_width", 1280);
      this->_image_height = this->get_param("image_height", 720);
      this->_port = this->get_param("port", 8554);
      this->_bitrate = this->get_param("bitrate", 1);
      
      logger.info("image_width: " + std::to_string(static_cast<int>(this->_image_width)));
      logger.info("image_height: " + std::to_string(static_cast<int>(this->_image_height)));
      logger.info("port: " + std::to_string(static_cast<int>(this->_port)));
      logger.info("bitrate: " + std::to_string(static_cast<int>(this->_bitrate)));


      // 初始化 推流分辨率(640, 480)，端口号8554，比特率1Mb
      this->_vs.setup(cv::Size(this->_image_width, this->_image_height), this->_port, this->_bitrate);
   
    }
    ~StreamerCppNode()
    {
    }
    void run();

    void full_res_callback(nlohmann::json msg)
    {
      // 放入到阻塞队列中
      {
        std::unique_lock<std::mutex> lock(this->_full_queue_mtx);
        this->_full_queue.push(msg);
      }
      // 通知主线程取数据
      this->_full_cv.notify_one();
    }

    int _image_width;
    int _image_height;
    int _port;
    int _bitrate;



    
    VideoStreamer _vs;

  private:
    // 订阅话题
    sms::Subscriber _det_res_sub;

    std::mutex _full_queue_mtx;
    std::queue<nlohmann::json> _full_queue;
    std::condition_variable _full_cv;



};

}
#endif
